<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">选项管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>选项管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <input id="option-edit-value" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="option-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="option-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
        </div>

        <!-- 数据表格 -->
        <table class="layui-table" id="option-table" lay-filter="option-table"></table>
    </div>
</div>

<!-- 表单弹窗 -->
<script type="text/html" id="option-model">
    <form id="option-form" lay-filter="option-form" class="layui-form model-form">
        <input name="optnid" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="layui-badge-dot"></span>选项名称</label>
            <div class="layui-input-block">
                <input name="opname" placeholder="选项名称" type="text" class="layui-input"
                       lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item model-form-footer">
            <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
            <button class="layui-btn" lay-filter="option-form-submit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="option-table-bar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
    layui.use(['form', 'table', 'config', 'admin'], function () {
        let form = layui.form;
        let table = layui.table;
        let config = layui.config;
        let layer = layui.layer;
        let admin = layui.admin;

        let url = config.base_server + 'api-paper/option/getOptions';
        let params = {
            access_token: config.getToken().access_token
        };

        let cols = [[
            {type: 'numbers'},
            {field: 'opname', sort: true, title: '选项名称'},
            {field: 'cretim', sort: true, title: '创建时间'},
            {field: 'updtim', sort: true, title: '修改时间'},
            {align: 'center', toolbar: '#option-table-bar', title: '操作', width: 120}
        ]];
        //渲染表格
        table.render({
            elem: '#option-table',
            url: url,
            where: params,
            page: true,
            cols: cols
        });

        // 添加按钮点击事件
        $('#option-btn-add').click(function () {
            showEditModel();
        });

        // 表单提交事件
        form.on('submit(option-form-submit)', function (data) {
            layer.load(2);
            admin.req('api-paper/option/saveOrUpdate', JSON.stringify(data.field), function (data) {
                layer.closeAll('loading');
                if (data.code === 0) {
                    layer.msg(data.msg, {icon: 1, time: 500});
                    table.reload('option-table');
                    layer.closeAll('page');
                } else {
                    layer.msg(data.msg, {icon: 2, time: 500});
                }
            }, $('#option-form').attr('method'));
            return false;
        });

        // 工具条点击事件
        table.on('tool(option-table)', function (obj) {
            let data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { //删除
                doDelete(obj);
            }
        });

        // 搜索按钮点击事件
        $('#option-btn-search').click(function () {
            let key = $('#option-edit-value').val();
            table.reload('option-table', {where: {searchKey: key}});
        });

        // 显示编辑弹窗
        let showEditModel = function (data) {
            layer.open({
                type: 1,
                title: data ? '修改选项' : '添加选项',
                content: $('#option-model').html(),
                area: '400px',
                offset: '200px',
                success: function () {
                    let optionForm = $('#option-form');
                    optionForm[0].reset();
                    optionForm.attr('method', 'POST');
                    if (data) {
                        let opname = $("input[name='opname']");
                        opname.attr('disabled', true);
                        opname.attr('class', "layui-input layui-disabled");
                        form.val('option-form', data);
                        optionForm.attr('method', 'POST');
                    }
                }
            });
        };

        // 删除
        let doDelete = function (obj) {
            layer.confirm('确定要删除吗？', function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('api-paper/option/delete/' + obj.data.optnid, {}, function (data) {
                    layer.closeAll('loading');
                    if (data.code === 0) {
                        layer.msg(data.msg, {icon: 1, time: 500});
                        obj.del();
                    } else {
                        layer.msg(data.msg, {icon: 2, time: 500});
                    }
                }, 'DELETE');
            });
        };
    });

</script>